package com.fengye.security.security.service;

import com.fengye.security.security.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsPasswordService;
import org.springframework.stereotype.Service;

/**
 * @author fengyexjtu@126.com
 * @date 2022年05月24日 3:10 PM
 */
@Service
@RequiredArgsConstructor
public class UserDetailsPasswordServiceImpl implements UserDetailsPasswordService {

    private final UserRepository userRepository;

    @Override
    public UserDetails updatePassword(UserDetails userDetails, String newPassword) {
        return userRepository.findUserByUsername(userDetails.getUsername())
                .map(user -> (UserDetails) userRepository.save(user.withPassword(newPassword)))
                .orElse(userDetails);
    }
}
